package com.liuke.study.thread;

import com.sun.corba.se.spi.orbutil.threadpool.Work;

/**
 * TODO
 *
 * @author wangjwy
 * @date 2019/10/18 10:02
 * @since 1.0.0
 */
public class Test1 {

	public static void main(String[] args) throws InterruptedException {
		Printer a = new Printer("A");
		Printer b = new Printer("B");
		Printer c = new Printer("C");
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					while (true)  {
						synchronized (c) {
							synchronized (a) {
								a.work();
								a.notify();
							}
							c.wait();
						}
					}
				} catch (Exception e) {

				}
			}
		}).start();

		Thread.sleep(100);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					while (true) {
						synchronized (a) {
							synchronized (b) {
								b.work();
								b.notify();
							}
							a.wait();
						}
					}
				} catch (Exception e) {

				}
			}
		}).start();

		Thread.sleep(100);

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					while (true) {
						synchronized (b) {
							synchronized (c) {
								c.work();
								c.notify();
							}
							b.wait();
						}
					}
				} catch (Exception e) {

				}
			}
		}).start();

		Thread.sleep(10000);
	}


}

class Worker implements Runnable {

	private Printer printer;

	public Worker(Printer printer) {
		this.printer = printer;
	}

	@Override
	public void run() {

		synchronized (printer) {
			printer.work();
		}
	}
}


class Printer {
	private String out;

	public Printer(String out) {
		this.out = out;
	}

	public void work() {
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.print(out);
	}

}